﻿using AM.Tools;
using FinalTestMachine.Connect;
using FinalTestMachine.Core.FileOperate;
using FinalTestMachine.Motion.Aixs;
using FinalTestMachine.Motion.IO;
using FinalTestMachine.Motion.Product;
using FinalTestMachine.View;
using NPOI.SS.Formula.PTG;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace FinalTestMachine.Motion.Process
{
    public class SocketSpotInspection : RM_dll2.Process.ProcessIPauseBase
    {
        public static readonly SocketSpotInspection Cur = new SocketSpotInspection();

        public SocketSpotInspection() : base("治具点检流程", StaticInitial.Motion, true)
        {
        }

        private bool[] result = new bool[4];

        private string GetResult()
        {
            var temStr = string.Empty;
            foreach (var item in result)
            {
                temStr += item + ",";
            }

            temStr = temStr.Trim(',');

            return temStr;
        }

        /// <summary>
        /// 治具点检模式
        /// </summary>
        protected override void ImplementRunAction()
        {
            string error = string.Empty;
            string startTime = string.Empty;
            string endTime = string.Empty;

            int index = 0;
            try
            {
                Ref_Step("遮光板气缸升起");

                while (true)
                {
                    WaitPause();

                    if (!IsProcessRunning)
                    {
                        break;
                    }

                    switch (Step)
                    {
                        case "遮光板气缸升起":

                            Ref_Step("工位三气缸升起");

                            break;

                        case "工位三气缸升起":
                            //光碗升降气缸到原点位
                            if (!StaticIOHelper.Cylinder_SiteBowlLightUpDown.OriginPos(out error))
                            {
                                WriteErrToUser(error);
                                break;
                            }

                            Ref_Step("工位三移栽轴移动到等待位");
                            break;

                        case "工位三移栽轴移动到等待位":
                            try
                            {
                                Aixs.Aix_Turn.Cur.Mov_0();
                            }
                            catch (Exception ex)
                            {
                                WriteErrToUser(ex.Message);
                                break;
                            }

                            startTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            Ref_Step("准直仪Z轴移动到工作位");

                            break;

                        case "准直仪Z轴移动到工作位":
                            Ref_Step("开始测试");
                            break;

                        case "开始测试":
                            //向准直仪发送测试命令
                            CollimatorTest.SendToStartMeasure();
                            Ref_Step("判断测试结果");

                            break;

                        case "判断测试结果":

                            if (Connect.CollimatorTest.IsFinished)
                            {
                                if (Connect.CollimatorTest.IsOK)
                                {
                                    result[index] = true;
                                    WriteToUser("准直仪测试 OK", Brushes.Green, false);
                                }
                                else
                                {
                                    result[index] = false;
                                    WriteToUser("准直仪测试 NG", Brushes.Red, false);
                                }

                                index++;
                                Ref_Step("旋转到下一工位");
                            }

                            break;

                        case "旋转到下一工位":
                            try
                            {
                                RotatedMovNextHelper.MovNext();
                            }
                            catch (Exception ex)
                            {
                                WriteErrToUser(ex.Message);
                                break;
                            }

                            if (index >= 4)
                            {
                                endTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                                var timeNow = DateTime.Now;
                                CsvHelper csvSocketDailyCheck = new CsvHelper(() => $"..//data//{timeNow.ToString("yyyy")}//{timeNow.ToString("yyyy-MM-dd")}//{WorkItemManagerHelper.LoadedName}", () => "SocketDailyCheck.csv");
                                csvSocketDailyCheck.SetCSVHeader("Programver", "Model", "StartTime", "EndTime", "MachineName", "Operator", "Result" + "\r\n",
                                                                 "程序版本号", "产品型号", "开始时间", "结束时间", "设备名称", "操作人员", "结果" + "\r\n",
                                                                 "プログラムバージョン番号", "製品型番", "開始時間", "終了時間", "デバイス名", "オペレータ", "結果＃ケッカ＃" + "\r\n",
                                                                  "-", "-", "-", "-", "-", "-", "-" + "\r\n", "-", "-", "-", "-", "-", "-", "-");

                                var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

                                string Operator = Wpf_LoginWnd.LoginHelper.Cur.CurUserMsg.Category.ToString() + " | " + Wpf_LoginWnd.LoginHelper.Cur.CurUserMsg.ID;

                                csvSocketDailyCheck.WriteLine(version, View.WorkItemManagerHelper.LoadedName, startTime, endTime, "FinalTestMachine", Operator, GetResult());

                                Ref_Step("退出");
                            }
                            else
                            {
                                Ref_Step("准直仪Z轴移动到工作位");
                            }

                            break;

                        case "退出":
                            System.Windows.MessageBox.Show($"治具点检结果：{GetResult()}");
                            IsProcessRunning = false;

                            break;
                    }

                    Sleep(20);
                }
            }
            catch (Exception ex)
            {
                WriteErrToUser("治具点检流程错误：" + ExceptionHelper.GetInnerExceptionMessageAndStackTrace(ex));
                WriteErrToUser("治具点检流程错误：" + ExceptionHelper.GetInnerExceptionMessage(ex));
            }
        }
    }
}